package com.wang.userscenter.common.util;

/**
 * 分页的工具类
 * 
 * @author Zjx
 */
public class PageInfoUtil {
	/**
	 * 总条数
	 */
	private int totalRecord;
	/**
	 * 每页条数
	 */
	private int pageSize = 20;
	/**
	 * 总页数
	 */
	private int totalPage;
	/**
	 * 当前页
	 */
	private int currentPage;
	/**
	 * 当前页的起始条数
	 */
	private int currentRecord;
	/**
	 * 上一页
	 */
	private int prePage;
	/**
	 * 下一页
	 */
	private int nextPage;

	public int getTotalRecord() {
		if (this.totalRecord < 0) {
			this.totalRecord = 0 ;
		}
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		/* 容错 */
		if (this.currentPage < 1 ) {
			this.currentPage = 1 ; 
		}
		if (this.currentPage > this.getTotalPage()) {
			this.currentPage = this.getTotalPage();
		}
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPage() {
		if (this.getTotalRecord() <= 0 ) {
			this.totalPage = 1 ;
		} else {
			/* 总页数如何计算;
			 * 条件:总条数,和每多少条	总页数
			 * 		100		20	 100 / 20 		
			 * 		23		10	 20 / 10 + 1 ;
			 * 共100条,每页20条,共5页;
			 * 页面	起始条数	结束条数
			 * 1	0		10
			 * 2	11		20
			 * 3	31		40
			 */
			int modNum = this.getTotalRecord() % this.getPageSize();
			/* 求商 */
			this.totalPage = this.getTotalRecord() / this.getPageSize();
			if (modNum != 0 ) {
				/* 未除尽 */
				this.totalPage = totalPage + 1;
			}
		}
		return totalPage;
	}

	public int getPrePage() {
		this.prePage = this.getCurrentPage() - 1 ; 
		/* 容错 */
		if (this.prePage < 1 ) {
			this.prePage = 1 ; 
		}
		return prePage;
	}

	public int getNextPage() {
		this.nextPage = this.getCurrentPage() + 1 ;
		if (this.nextPage > this.getTotalPage()) {
			this.nextPage = this.getTotalPage();
		}
		return nextPage;
	}
	
	/**
	 * 每页10条
	 * 页面	起始条数	结束条数
	 * 1	1		10		(当前页-1)*每页条数;
	 * 2	11		20
	 * 3	31		40
	 * @return
	 */
	public int getCurrentRecord() {
		this.currentRecord = (this.getCurrentPage() - 1 )* this.pageSize ;
		return currentRecord;
	}

	@Override
	public String toString() {
		return "PageInfoUtil [totalRecord=" + this.getTotalRecord() + ", pageSize=" + this.getPageSize() 
				+ ", totalPage=" + this.getTotalPage() + ", currentPage=" 
				+ this.getCurrentPage() + ", prePage=" + this.getPrePage() 
				+ ", nextPage=" + this.getNextPage() + ",getCurrentRecord:" + this.getCurrentRecord() + "]";
	}
	
	public static void main(String[] args) {
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		pageInfoUtil.setTotalRecord(100);
		pageInfoUtil.setPageSize(10);
		pageInfoUtil.setCurrentPage(3);
		System.out.println("====" + pageInfoUtil);
	}
}
